<?php

/**
 * @filesource Communicates with the Contact control file to know which student
 * data to get and set. It is the only file that has the SQL queries and MySQL
 * connections
 *
 * @copyright 2011 by Dan, Steven, Brice, Mike, and Aaron.  This program is part
 * of Internship 2 Career, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version February 6, 2011
 *
 * @author Brice Rader and Aaron Jarzombeck
 */
/**
 * This is the database file.
 * It controls the major file to list all majors and
 * to add or delete a major into the database.
 * It is the only file that has the SQL queries and MySQL connections
 *
 */

class dbMajor {

    private $db;

    static function createTable() {
        //Create table major
        $query = "
            create table major(
                majorName    varchar(45) NOT NULL,
                constraint PK_Major PRIMARY KEY (majorName)
                );
            ";
        connectToDatabase();
        $result = mysql_query($query);
        mysql_close();
        if ($result == false) {
            echo mysql_error();
            return false;
        } else {
            connectToDatabase();
            mysql_query("INSERT INTO major VALUES('Biology')");
            mysql_query("INSERT INTO major VALUES('CA')");
            mysql_query("INSERT INTO major VALUES('CIS')");
            mysql_query("INSERT INTO major VALUES('English')");
            mysql_query("INSERT INTO major VALUES('Hisotry')");
            mysql_query("INSERT INTO major VALUES('PS')");
            mysql_query("INSERT INTO major VALUES('Psyc')");
            mysql_query("INSERT INTO major VALUES('Unknown')");
            mysql_close();
        }

        //Previous query was successful.
        return true;
    }

    /**
     * Get's all of the majors in the database
     * @return majors
     */
    function getAllMajors() {
        $query = "SELECT * FROM major";
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close();
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return $result;
    }

    /**
     * Adds major to database
     * @param major $m
     * @return major
     */
    static function addMajor($m, &$id) {
        if (!$m instanceof major) {
            echo ("Invalid arguement insertInto ( ) function<br />");
            return false;
        }
        //Compose the INSERT INTO query srting. Primary key value is missing.
        //Database server automatically generates a unique ID for the primary
        //key.
        $query = sprintf("INSERT INTO major VALUES ('%s')",
                        $m->getName()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close( );
        if ($result == false) {
            echo mysql_error ( );
            return false;
        }
        //query was successful.
        printf("Major added has id %d\n<br />", mysql_insert_id());
        return true;
    }

    /**
     * Checks the majors in the database
     * @param major $m
     * @return major 
     */
    function checkName($m) {
        if (!($m instanceof Major)) {
            return false;
        }
        $majorName = $m->getName();
        connectToDatabase();
        if (mysql_num_rows(mysql_query("SELECT * FROM major WHERE majorName=
                $majorName"))
                == false) {
            mysql_close();
            //nothing existsmysql_num_rows
            return false;
        } else {
            return true;
        }
    }

    /**
     * Gets the major that user calls
     * @param $name
     * @return Major 
     */
    function getMajor($majorName) {
        $m = new Major ();
        $result = mysql_query("SELECT * FROM major WHERE majorName=$majorName");
        if (!$result) {
            return false; //name does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $m->setName($majorName);
            return $m;
        }
    }

    /**
     * Deletes major from database
     * @param major $m
     * @return major in database
     */
    function deleteMajor($m) {
        if (!$m instanceof Major) {
            echo ("Invalid argument deleteFrom( ) function<br />");
            return false;
        }

        //Compose the DELETE FROM query string.
        $query = sprintf("DELETE FROM major WHERE majorName = '%s'",
                        $m->getName()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close( );
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return true;
    }


}

?>
